package calorie.com.calorie.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.LoginContact;
import calorie.com.calorie.response.ResultDTO;
import calorie.com.calorie.response.User;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;

;

/**
 * 启动页
 */
public class SplashActivity extends MvpActivity<LoginContact.LoginPresent> implements LoginContact.ILoginView {

    @Override
    public void initData(Bundle savedInstanceState) {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                String account = SPUtils.getString(Constant.ACCOUNT, null, context);
                String loginPwd = SPUtils.getString(Constant.LOGIN_PWD, null, context);
                if (!ObjectUtils.isEmpty(account) && !ObjectUtils.isEmpty(loginPwd)) {
                    mPresent.login(context, account, loginPwd);
                } else {
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                    finish();
                }
            }
        }, 1000);
    }

    @Override
    public int getLayoutId() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉手机顶部状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//适配华为虚拟下面导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        return R.layout.activity_splash_after;
    }


    @Override
    protected LoginContact.LoginPresent createPresent() {
        return new LoginContact.LoginPresent();
    }

    @Override
    public void getLoginData(Response<ResultDTO> response) {
        boolean autoLogin = SPUtils.getBoolean(Constant.AUTOLOGIN, false, context);
        if ("200".equals(response.body().getCode()) && autoLogin) {
            Gson gs = new Gson();
            StaticValue.user = gs.fromJson(gs.toJson(response.body().getData()), User.class);
            if (StaticValue.user.getMobilePhone() == null || StaticValue.user.getMobilePhone().equals("")) {
                ToastUtils.showLongToast(context, "账号异常，请重新设置登录信息");
                startActivity(new Intent(context, FirstLoginModifyPwdActivity.class));
                this.finish();
            } else {
                startActivity(new Intent(context, MainActivity.class));
                SPUtils.putString(Constant.LOGIN_TIME, StaticValue.user.getLoginTime(), context);
                SPUtils.putString(Constant.RECEIVE_NAME,StaticValue.user.getReceivingName(),context);
                SPUtils.putString(Constant.RECEIVE_PHONE,StaticValue.user.getReceivingPhone(),context);
                SPUtils.putString(Constant.RECEIVE_ADDRESS,StaticValue.user.getReceivingAddress(),context);
                this.finish();
            }

        } else {

            startActivity(new Intent(context, LoginActivity.class));
        }
        finish();
    }

    @Override
    public void getDataFailed() {
        startActivity(new Intent(context, LoginActivity.class));
        finish();
    }

}
